<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 3.9.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">

<link rel="stylesheet" href="https://fonts.loli.net/css?family=Lato:300,300italic,400,400italic,700,700italic&display=swap&subset=latin,latin-ext">
<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"monocy.gitee.io","root":"/","scheme":"Gemini","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":true,"show_result":true,"style":"mac"},"back2top":{"enable":true,"sidebar":true,"scrollpercent":true},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":true,"lazyload":true,"pangu":false,"comments":{"style":"tabs","active":"valine","storage":true,"lazyload":true,"nav":null,"activeClass":"valine"},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":false,"top_n_per_article":5,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="前言Form 是项目中经常使用的组件，为了能熟练驾驭它，深入 Form 源码还是有所必要的。我们知道，在 Ant Design 4 之后，Form 的底层使用的是 rc-field-form，因此在了解源码时，我们也得对其底层的依赖深挖一番，这样才能了解的更透彻。">
<meta name="keywords" content="Form,AntD">
<meta property="og:type" content="article">
<meta property="og:title" content="深入 AntD Form">
<meta property="og:url" content="https://monocy.gitee.io/2022/07/26/深入-AntD-Form/index.html">
<meta property="og:site_name" content="沐光">
<meta property="og:description" content="前言Form 是项目中经常使用的组件，为了能熟练驾驭它，深入 Form 源码还是有所必要的。我们知道，在 Ant Design 4 之后，Form 的底层使用的是 rc-field-form，因此在了解源码时，我们也得对其底层的依赖深挖一番，这样才能了解的更透彻。">
<meta property="og:locale" content="zh-CN">
<meta property="og:updated_time" content="2022-07-26T13:14:08.423Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="深入 AntD Form">
<meta name="twitter:description" content="前言Form 是项目中经常使用的组件，为了能熟练驾驭它，深入 Form 源码还是有所必要的。我们知道，在 Ant Design 4 之后，Form 的底层使用的是 rc-field-form，因此在了解源码时，我们也得对其底层的依赖深挖一番，这样才能了解的更透彻。">

<link rel="canonical" href="https://monocy.gitee.io/2022/07/26/深入-AntD-Form/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>深入 AntD Form | 沐光</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">沐光</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">记录在前端之路的点点滴滴</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签<span class="badge">31</span></a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类<span class="badge">15</span></a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档<span class="badge">61</span></a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="reading-progress-bar"></div>

  <a href="https://github.com/kazehaiya" class="github-corner" title="Follow me on GitHub" aria-label="Follow me on GitHub" rel="noopener" target="_blank"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://monocy.gitee.io/2022/07/26/深入-AntD-Form/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="蔡胤">
      <meta itemprop="description" content="Just do IT!">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="沐光">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          深入 AntD Form
        </h1>

        <div class="post-meta">
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2022-07-26 21:14:08" itemprop="dateModified" datetime="2022-07-26T21:14:08+08:00">2022-07-26</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/源码剖析/" itemprop="url" rel="index"><span itemprop="name">源码剖析</span></a>
                </span>
            </span>

          
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="far fa-comment"></i>
      </span>
      <span class="post-meta-item-text">Valine：</span>
    
    <a title="valine" href="/2022/07/26/深入-AntD-Form/#valine-comments" itemprop="discussionUrl">
      <span class="post-comments-count valine-comment-count" data-xid="/2022/07/26/深入-AntD-Form/" itemprop="commentCount"></span>
    </a>
  </span>
  
  
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="far fa-file-word"></i>
              </span>
                <span class="post-meta-item-text">本文字数：</span>
              <span>3.7k</span>
            </span>
            <span class="post-meta-item" title="阅读时长">
              <span class="post-meta-item-icon">
                <i class="far fa-clock"></i>
              </span>
                <span class="post-meta-item-text">阅读时长 &asymp;</span>
              <span>3 分钟</span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h2 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h2><p><code>Form</code> 是项目中经常使用的组件，为了能熟练驾驭它，深入 <code>Form</code> 源码还是有所必要的。我们知道，在 <code>Ant Design 4</code> 之后，<code>Form</code> 的底层使用的是 <code>rc-field-form</code>，因此在了解源码时，我们也得对其底层的依赖深挖一番，这样才能了解的更透彻。</p>
<a id="more"></a>
<h2 id="rc-field-form"><a href="#rc-field-form" class="headerlink" title="rc-field-form"></a>rc-field-form</h2><blockquote>
<p>💻 仓库地址：<a href="https://github.com/react-component/field-form" target="_blank" rel="noopener">【传送门】</a>（建议动手运行一下，会了解的更快）</p>
<p>⚠️ 注：表单的所有操作都离不开顶层的 <code>FormStore</code>，所有的变动信息最终都会汇聚在 <code>FormStore</code> 内。</p>
</blockquote>
<h3 id="整体结构"><a href="#整体结构" class="headerlink" title="整体结构"></a>整体结构</h3><p>在了解源码之前，我们先大致了解一下 <code>rc-field-form</code> 的结构，因为 <code>Ant Design Form</code> 是基于此封装的，因此熟悉 <code>rc-field-form</code> 对我们解读源码来说很重要。</p>
<p>页面渲染的结构树如下：</p>
<p><img data-src="/images/antd-form/rcFormComponentStructure.png" alt="./rcFormComponentStructure"></p>
<p>基于此，我们使用 <code>ReactDom</code> 的方式用代码描述大致为：</p>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 顶层 Form 的大致样式</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="comment">// 手动添加 Provider 并初始化部分能力</span></span><br><span class="line">&lt;Form.Provider &#123;...props&#125;&gt;</span><br><span class="line">  <span class="comment">// 重写原生 form 的部分事件能力</span></span><br><span class="line">  &lt;form &#123;...props&#125; onSubmit=&#123;xxx&#125; onReset=&#123;xxx&#125;&gt;</span><br><span class="line">    <span class="comment">// FieldContext 传递顶层的 FormContext 内容，方便便捷获取(包括 formInstance</span></span><br><span class="line">    对象和 validator 方法) <span class="comment">// childNode 对 function 和 JSX.Element 做了兼容处理</span></span><br><span class="line">    &lt;FieldContext.Provider value=&#123;formContextValue&#125;&gt;</span><br><span class="line">      <span class="comment">// WrapperField 用于获取 FieldContext、拼接 name、类型等基础操作</span></span><br><span class="line">      &lt;WrapperField&gt;</span><br><span class="line">        <span class="comment">// Field 层做 Dom 元素重写（elementClone 方法）、校验方法封装、props</span></span><br><span class="line">        属性扩展等能力 <span class="comment">// 值的变动、获取等能力都是从顶层 Store 传下来的</span></span><br><span class="line">        &lt;Field&gt;&#123;element&#125;&lt;<span class="regexp">/Field&gt;</span></span><br><span class="line"><span class="regexp">      &lt;/</span>WrapperField&gt;</span><br><span class="line">    &lt;<span class="regexp">/FieldContext.Provider&gt;</span></span><br><span class="line"><span class="regexp">  &lt;/</span>form&gt;</span><br><span class="line">&lt;<span class="regexp">/Form.Provider&gt;</span></span><br></pre></td></tr></table></figure>
<h3 id="结构解析"><a href="#结构解析" class="headerlink" title="结构解析"></a>结构解析</h3><p>根据 <code>rc-field-form</code> 源码的渲染逻辑，这里我花了一张结构图：</p>
<p><img data-src="/images/antd-form/rcFormStructure.png" alt="rcFormStructure"></p>
<p>其渲染的步骤大致就是：</p>
<ol>
<li>通过 <code>useForm</code> 创建顶层的 <code>FormStore</code> 存储库，然后通过 <code>FieldContext</code> 向下传递；</li>
<li><code>Field</code> 获取到由 <code>FieldContext</code> 传下的顶层 <code>FormStore</code> ，获取需要的 <code>name</code> 和一些操作方法，对 <code>DomeElement</code> 做克隆和属性封装；</li>
<li>根据最终传入的 <code>Dom</code> 类型，做相应的格式化处理和呈现；（有可能传入的为函数）</li>
</ol>
<h3 id="能力拆解"><a href="#能力拆解" class="headerlink" title="能力拆解"></a>能力拆解</h3><p>整个 <code>rc-field-form</code> 的能力可简单拆解为：</p>
<ol>
<li><strong><code>数据存储</code></strong>：在 <code>Form</code> 顶层维护了一个 <code>Context</code> 存储库，对 <code>Form</code> 内注册的任何子表单项的变动，都通过注册好的 <code>get</code>、<code>set</code> 方法来存入 <code>store</code> 内；</li>
<li><strong><code>表单对象</code></strong>：顶层通过 <code>useRef</code> 生成一个不随组件更新的 <code>formInstance</code> 对象，然后通过 <code>useForm</code> 生成，当初次渲染时创建出 <code>FormStore</code> 绑定即可，后续如果需要刷新则触发初始化绑定的 <code>forceUpdate</code> 方法即可刷新（我们通过 <code>form</code> 拿到的表单对象就是这个 <code>formInstance</code>）；</li>
<li><strong><code>表单校验</code></strong>：使用 <code>async-validate</code> 插件库校验，拼接格式则是通过 <code>name</code> + <code>rules</code> 字段来生成（组件注册阶段时生成组件描述数组即能方便的获取结构）；</li>
<li><strong><code>变更事件</code></strong>：表单项的变更事件是在注册 <code>Field</code> 时绑定的，将传入的 <code>children</code> 进行 <code>cloneElement</code> 操作，生成目标子元素并改写 <code>props</code>（通过 <code>getControlled</code> 方法去改写 <code>props</code> 以及增加校验等操作）。</li>
<li><strong><code>命名获取</code></strong>：表单项有个 <code>name</code> 字段是用于定义表单名的，层级越深的表单要设置其 <code>name</code> 时，需要通过 <code>getNamePath</code> 方法来拼接成最终的 <code>name</code>，最终 join 生成的 <code>namePath</code> 大致为 <code>prefixPath_currentName</code></li>
</ol>
<p>其中，<code>formInstance</code> 暴露出来的能力为（顶层 <code>FormStore</code> 暴露的能力）：</p>
<p><img data-src="/images/antd-form/rcFormInstance.png" alt="rcFormInstance"></p>
<p>而这也可以算是 <code>rc-field-form</code> 对外暴露的所有能力了（内部拼接 <code>name</code> 的方法是 <code>getNamePath</code>，建议留心一下）。</p>
<h2 id="深入-AntD-Form"><a href="#深入-AntD-Form" class="headerlink" title="深入 AntD Form"></a>深入 AntD Form</h2><h3 id="AntD-Form-组件及能力"><a href="#AntD-Form-组件及能力" class="headerlink" title="AntD Form 组件及能力"></a><code>AntD Form</code> 组件及能力</h3><p><img data-src="/images/antd-form/antdForm.png" alt="antdForm"></p>
<p>图中有 <code>AntD</code> 在 <code>4.20</code> 支持的新特性：<code>useWatch</code>、<code>useFormInstance</code>：</p>
<ul>
<li><p><code>useFormInstance</code>：返回 <code>useForm</code> 获取的 <code>form</code> 对象，这对于子组件拿顶层 <code>form</code> 对象来说很方便；</p>
</li>
<li><p><code>useWatch</code>：监听字段变化返回更新后的值，仅当改字段变化时重新渲染（内部用 <code>useEffect</code> 做初始化赋值）。</p>
</li>
</ul>
<blockquote>
<p>⏰ 暴露的 <code>Form.Provider</code> 一般自己封装，相当于就是使用 <code>rc-field-form</code> 自己手写 <code>Provider</code> 层一样，一般用不着。</p>
</blockquote>
<h3 id="Form-结构"><a href="#Form-结构" class="headerlink" title="Form 结构"></a><code>Form</code> 结构</h3><p><code>AntD Form</code> 整体基于 <code>rc-field-form</code> 封装了一层，其拓展了对于 <code>AntD</code> 其余组件使用的能力和样式的填装。我们看个简单例子：</p>
<p><img data-src="/images/antd-form/antdFormComponentStructure.png" alt="./antdFormComponentStructure"></p>
<p>这里大致可分为四层：</p>
<ul>
<li><code>Form Provider</code>：此部分实现了 <code>rc-field-form</code> 需要手写的部分；</li>
<li><code>Custom Provider</code>：此部分主要为 <code>AntD</code> 需要的样式控制的 <code>Context</code>；</li>
<li><code>Field Provider</code>：沿用 <code>rc-field-form</code> 的 <code>FieldContext</code>，添加了一些“错误呈现”、“dependencies 依赖”等内容；</li>
<li><code>Content</code>：主要做值绑定和一些校验结果、样式等的呈现，内部有自定义的关联 <code>Context</code>；</li>
</ul>
<h3 id="InternalForm-相关内容"><a href="#InternalForm-相关内容" class="headerlink" title="InternalForm 相关内容"></a><code>InternalForm</code> 相关内容</h3><p><strong>A. 内部调用的 <code>useForm</code> 封装了什么？</strong></p>
<p><img data-src="/images/antd-form/useForm.png" alt="useForm"></p>
<p><code>useForm</code> 额外增添的内容不多，一个是 <code>__INTERNAL__</code>，它的作用就是为 <code>React.forwardRef</code> 暴露通过 <code>useForm</code> 生成的对象（可以了解一下 <code>useImperativeHandle</code>）。</p>
<p><strong>B. 它在 <code>Form</code> 的基础上又做了什么？</strong></p>
<ol>
<li>初始化了很多关于样式控制相关的内容（这里将 disable、layout、size 等配置均归属于样式配置）；</li>
<li>向外暴露 ref 对象；</li>
<li>添加 <code>scroll</code> 错误定位；</li>
<li>自动绑定 <code>rc-field-form</code> 需要用户写的 <code>Provider</code>；</li>
</ol>
<h3 id="FormItem-相关内容"><a href="#FormItem-相关内容" class="headerlink" title="FormItem 相关内容"></a><code>FormItem</code> 相关内容</h3><p><strong>A. <code>FormItem</code> 主要作用</strong></p>
<ol>
<li>决定表单的呈现方式：<code>函数式</code>、<code>组件式</code>、<code>数组</code>；</li>
<li>组件更新判断，局部更新处理；</li>
<li>校验相关的绑定；</li>
</ol>
<blockquote>
<p>总而言之，就是将 <code>Field</code> 组件做 <code>AntD</code> 的本地化处理（通过函数子组件获取 <code>Field</code> 解析好的所有配置）。</p>
</blockquote>
<p><strong>B. <code>FormList</code> 的对 <code>FormItem</code> 的封装有什么？</strong></p>
<p><code>FormList</code> 其实就是调用 <code>rc-field-form</code> 的列表操作，然后让前端自己去对应封装 <code>FormItem</code>。</p>
<p><code>rc-field-form</code> 的 <code>List</code> 组件则是对外暴露了一个函数（我们用官网 <code>List</code> 组件常用的那个函数），它最终操作的就是 <code>Field</code> 组件暴露出来的能力。</p>
<h2 id="可动手的点"><a href="#可动手的点" class="headerlink" title="可动手的点"></a>可动手的点</h2><p>从 <code>AntD</code> 对外暴露的能力来看，我们基本没法基于它包装内容，因为它基本上就是包装 <code>rc-field-form</code>，却又没有对外面暴露 <code>rc-field-form</code> 支持的能力。因此通过锁定版本，根据其依赖的 <code>rc-field-form</code> 版本来扩充一些能力，而这就是 <code>@alife/form-tools</code> 封装出的原因。目前业务上使用表单比较难受的点有：</p>
<ul>
<li><code>form</code> 对象的深层传入，深层次组件的 <code>namePath</code> 不好监听（4.20.0 版本已支持）。</li>
<li>对于兄弟组件间的事件消息处理的很难受，特别是值变更事件（4.20.0 版本已支持）；</li>
<li>对象类表单数据的<code>数组命名法</code>很难受，无法复用表单组件；</li>
<li>复杂表单的初始化逻辑均收敛在上层，一些需要根据值做接口查询呈现的组件开发起来很困难；</li>
</ul>
<p>那么基于这些难点，我们根据 <code>rc-field-form</code> 内部暴露的一些能力可做一些封装。</p>
<h3 id="form-对象跨层级传递"><a href="#form-对象跨层级传递" class="headerlink" title="form 对象跨层级传递"></a><code>form</code> 对象跨层级传递</h3><p><code>4.20</code> 版本解决了跨层级传递 <code>form</code> 对象的问题，直接使用 <code>Form.useFormInstance</code> 获取到 <code>form</code> 对象，不用为之前需要通过 <code>props</code> 传值而烦恼了。</p>
<blockquote>
<p>🌰 使用示例：<a href="https://ant.design/components/form-cn/#Form.useFormInstance" target="_blank" rel="noopener">示例传送门</a></p>
</blockquote>
<p>但是它也有缺陷：</p>
<ul>
<li>获取 <code>form</code> 对象就得用到全量的 <code>NamePath</code>，难做到表单组件复用。</li>
</ul>
<blockquote>
<p>☕️ 如果需要将表单组件抽离出来做公共组件（它们仅包裹层的 <code>name</code> 不同），那么内部使用 <code>form</code> 的一些方法就会很难受（因为没法知道它的 <code>prefixName</code> 到底是什么）。</p>
</blockquote>
<p>因此可基于 <code>rc-field-form</code> 做一层优化：直接获取当前路径下的 <code>form</code> 对象，即“<strong>自动前缀补全</strong>”。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br></pre></td><td class="code"><pre><span class="line">import &#123; useCallback, useContext, useMemo &#125; from &apos;react&apos;;</span><br><span class="line">import &#123; InternalFormInstance, InternalNamePath, NamePath &#125; from &apos;rc-field-form/es/interface&apos;;</span><br><span class="line">import FieldContext from &apos;rc-field-form/es/FieldContext&apos;;</span><br><span class="line">import &#123; FormInstance &#125; from &apos;antd/es/form&apos;;</span><br><span class="line"></span><br><span class="line">function getLocalForm(form: InternalFormInstance, prefixPath: InternalNamePath = []) &#123;</span><br><span class="line">  // 获取所有值</span><br><span class="line">  function getLocalFieldValue(name: NamePath) &#123;</span><br><span class="line">    return originForm.getFieldValue([...prefixPath, ...getNamePath(name)]);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  // 获取单一值</span><br><span class="line">  function getLocalFieldsValue(</span><br><span class="line">    nameList?: NamePath[] | true,</span><br><span class="line">    filterFunc?: (meta: Meta) =&gt; boolean,</span><br><span class="line">  ) &#123;</span><br><span class="line">    if (!nameList || nameList === true) &#123;</span><br><span class="line">      const value = originForm.getFieldsValue([prefixPath], filterFunc);</span><br><span class="line">      return getValue(value, fullPrefix);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    // 为每个字段补全前缀</span><br><span class="line">    const formatNameList = nameList.map(nameItem =&gt; [...prefixPath, nameItem]);</span><br><span class="line">    const value = originForm.getFieldsValue(formatNameList, filterFunc);</span><br><span class="line">    return getValue(value, fullPrefix);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  // 设置所有值</span><br><span class="line">  // ...</span><br><span class="line"></span><br><span class="line">  // 设置单一值</span><br><span class="line">  // ...</span><br><span class="line"></span><br><span class="line">  // 重置当前表单</span><br><span class="line">  // ...</span><br><span class="line"></span><br><span class="line">  // 其它...（基本照着 FormStore 基本重写一遍即可）</span><br><span class="line"></span><br><span class="line">  return &#123;</span><br><span class="line">    getLocalFieldValue,</span><br><span class="line">    getLocalFieldsValue,</span><br><span class="line">    // ...</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">function useLocalForm() &#123;</span><br><span class="line">  const context = useContext(FieldContext);</span><br><span class="line">  const prefixPath = context.prefixName || [];</span><br><span class="line"></span><br><span class="line">  const localForm = useMemo(() =&gt; getLocalForm(context, prefixPath), [context, prefixPath])</span><br><span class="line"></span><br><span class="line">  return useMemo(() =&gt; &#123;</span><br><span class="line">    return [localForm] as const;</span><br><span class="line">  &#125;, [localForm]);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">export default useLocalForm;</span><br></pre></td></tr></table></figure>
<p>这样即使组件拆分出来，也可以不用关注 <code>Form</code> 命名前缀的问题，直接使用即可（数组在套用了 <code>FormAccess</code> 之后也适用此方法）。</p>
<h3 id="兄弟组件事件处理"><a href="#兄弟组件事件处理" class="headerlink" title="兄弟组件事件处理"></a>兄弟组件事件处理</h3><p>我们知道，如果要获取到兄弟组件间值的变更，设置此类事件监听方法很是头疼，不过在 <code>4.20</code> 版本提供的 <code>useWatch</code> 方法能够比较方便的解决这个问题，同时其获取值的能力是通过 <code>useEffect</code> 获取的，初始化时也能监听到变更。</p>
<blockquote>
<p>🌰 使用示例：<a href="https://ant.design/components/form-cn/#Form.useWatch" target="_blank" rel="noopener">示例传送门</a></p>
</blockquote>
<p><code>useWatch</code> 的缺陷（同 4.1 所述问题）</p>
<ul>
<li>一旦需要复用组件，限定死的 <code>NamePath</code> 会导致组件最终耦合太大，无法拆分。</li>
</ul>
<p>因此同样可按照 4.1 中的处理方式，注册个虚拟的 <code>Field</code> ，然后通过它来做事件监听，从而模拟 <code>dependencies</code> 的能力：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">useEffect(() =&gt; &#123;</span><br><span class="line">  const &#123; registerField &#125; = getInternalHooks(HOOK_MARK) as InternalHooks;</span><br><span class="line"></span><br><span class="line">  // 模拟 Field 的功能，创建一个虚拟的 Field 并绑定监听事件</span><br><span class="line">  const unregister = registerField(new FakeField(handleChange, &#123;</span><br><span class="line">    names: dependenciesCache.current.map((dep) =&gt; getNamePath(dep)),</span><br><span class="line">    trigger,</span><br><span class="line">    requirePathExist,</span><br><span class="line">  &#125;));</span><br><span class="line"></span><br><span class="line">  return () =&gt; &#123;</span><br><span class="line">    unregister();</span><br><span class="line">  &#125;;</span><br><span class="line">&#125;, [requirePathExist, handleChange, getInternalHooks, dynamic &amp;&amp; dependencies]);</span><br></pre></td></tr></table></figure>
<h3 id="更为舒适的命名方式"><a href="#更为舒适的命名方式" class="headerlink" title="更为舒适的命名方式"></a>更为舒适的命名方式</h3><p>先前提到过，<code>FormItem</code> 内的表单 <code>name</code> 值的获取其实是用 <code>Field</code> 的函数方式来获取到 <code>prefixName</code>，最终拼接好 <code>props</code> 上传入的 <code>name</code> 来设置全名的，整体的使用很被动，这就导致：</p>
<ul>
<li><p>获取值、监听事件等都需要拼接全名，对于复杂表单来说很麻烦；</p>
</li>
<li><p>数组形式的命名，会使得表单难以解耦，难以做到表单模块的复用；</p>
</li>
</ul>
<p><code>rc-field-form</code> 拼接名称的方式：</p>
<blockquote>
<p><code>WrapperField</code> 获取到当前 <code>fieldContext</code> 的 <code>prefixName</code> 值，再传入 <code>Field</code> 组件根据传入的 <code>name</code> 来拼接；</p>
</blockquote>
<p>既然它的 <code>prefixName</code> 是来自 <code>fieldContext</code> 的，而 <code>fieldContext</code> 的 <code>value</code> 又来自 <code>formContextValue</code>，且 <code>fieldContext</code> 内部并没包裹额外逻辑，那么我们为何在包裹表单对象的时候再包一层 <code>FieldContext</code>，直接敲定它的 <code>prefixName</code>，这样不就能优化掉 <code>Form.Item</code> 的数组命名方式吗？</p>
<p><strong><code>@alife/form-tools</code> 的功能拓展参考</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * 可参考 Field 的 WrapperField 的写法来写</span><br><span class="line"> **/</span><br><span class="line">import React, &#123; PropsWithChildren, useContext, useMemo &#125; from &apos;react&apos;;</span><br><span class="line">import FieldContext, &#123; HOOK_MARK &#125; from &apos;rc-field-form/es/FieldContext&apos;;</span><br><span class="line">import &#123; getNamePath &#125; from &apos;rc-field-form/es/utils/valueUtil&apos;;</span><br><span class="line">import &#123; NamePath &#125; from &apos;rc-field-form/es/interface&apos;;</span><br><span class="line"></span><br><span class="line">interface FormAccessProps &#123;</span><br><span class="line">  /* 拼接的 name 前缀，将 [objName, keyName] 的 objName 单独拆出来 */</span><br><span class="line">  name: NamePath;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">const FormAccess = (props: PropsWithChildren&lt;FormAccessProps&gt;) =&gt; &#123;</span><br><span class="line">  const &#123; name, children &#125; = props;</span><br><span class="line">  const context = useContext(FieldContext);</span><br><span class="line"></span><br><span class="line">  const value = useMemo(() =&gt; &#123;</span><br><span class="line">    return &#123;</span><br><span class="line">      ...context,</span><br><span class="line">      // 主要目的就是将 prefixName 尾部添加当前的 name 字段</span><br><span class="line">      prefixName: [</span><br><span class="line">        ...(context.prefixName || []),</span><br><span class="line">        ...getNamePath(name),</span><br><span class="line">      ],</span><br><span class="line">    &#125;;</span><br><span class="line">  &#125;, [context, name]);</span><br><span class="line"></span><br><span class="line">  // 重新包一个新的 Provider</span><br><span class="line">  return (</span><br><span class="line">    &lt;FieldContext.Provider value=&#123;value&#125;&gt;</span><br><span class="line">      &#123;children&#125;</span><br><span class="line">    &lt;/FieldContext.Provider&gt;</span><br><span class="line">  );</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">export default FormAccess;</span><br></pre></td></tr></table></figure>
<p><strong>使用方法示例</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line">// 值为： &#123; obj: &#123; test: xxx &#125; &#125;</span><br><span class="line">&lt;FormAccess name=&quot;obj&quot;&gt;</span><br><span class="line">  &lt;Form.Item name=&quot;test&quot;&gt;</span><br><span class="line">  	// ...</span><br><span class="line">  &lt;/Form.Item&gt;</span><br><span class="line">&lt;/FormAccess&gt;</span><br><span class="line"></span><br><span class="line">// 值为 &#123; arr: [] &#125;;</span><br><span class="line">&lt;Form.List name=&quot;test&quot;&gt;</span><br><span class="line">  &#123;(fields, operations) =&gt; &#123;</span><br><span class="line">    // 做一些逻辑封装</span><br><span class="line">    const &#123; add, remove, move &#125; = operations;</span><br><span class="line"></span><br><span class="line">    return fields.map((field) =&gt; &#123;</span><br><span class="line">      // fieldKey === key</span><br><span class="line">      const &#123; name, fieldKey, key &#125; = field;</span><br><span class="line"></span><br><span class="line">      // 简化 name 的赋值，children 部分可以抽离成公用模块</span><br><span class="line">      return (</span><br><span class="line">        &lt;FormAccess key=&#123;key&#125; name=&#123;name&#125;&gt;</span><br><span class="line">          &lt;Form.Item name=&quot;name&quot;&gt;</span><br><span class="line">            &lt;Input /&gt;</span><br><span class="line">          &lt;/Form.Item&gt;</span><br><span class="line">        &lt;/FormAccess&gt;</span><br><span class="line">      );</span><br><span class="line">    &#125;);</span><br><span class="line">  &#125;&#125;</span><br><span class="line">&lt;/Form.List&gt;</span><br></pre></td></tr></table></figure>
<h2 id="参考文章"><a href="#参考文章" class="headerlink" title="参考文章"></a>参考文章</h2><ul>
<li><a href="https://www.jianshu.com/p/f44bb8b16e58" target="_blank" rel="noopener">简单实现 rc-field-form</a></li>
</ul>

    </div>

    
    
    
        

<div>
<ul class="post-copyright">
  <li class="post-copyright-author">
    <strong>本文作者： </strong>蔡胤
  </li>
  <li class="post-copyright-link">
    <strong>本文链接：</strong>
    <a href="https://monocy.gitee.io/2022/07/26/深入-AntD-Form/" title="深入 AntD Form">https://monocy.gitee.io/2022/07/26/深入-AntD-Form/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" rel="noopener" target="_blank"><i class="fab fa-fw fa-creative-commons"></i>BY-NC-SA</a> 许可协议。转载请注明出处！
  </li>
</ul>
</div>


      <footer class="post-footer">
          
          <div class="post-tags">
              <a href="/tags/Form/" rel="tag"><i class="fa fa-tag"></i> Form</a>
              <a href="/tags/AntD/" rel="tag"><i class="fa fa-tag"></i> AntD</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2022/06/26/typeorm-操作-mongo-的方法小记/" rel="prev" title="typeorm 操作 mongo 的方法小记">
      <i class="fa fa-chevron-left"></i> typeorm 操作 mongo 的方法小记
    </a></div>
      <div class="post-nav-item"></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          
    <div class="comments" id="valine-comments"></div>

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#前言"><span class="nav-number">1.</span> <span class="nav-text">前言</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#rc-field-form"><span class="nav-number">2.</span> <span class="nav-text">rc-field-form</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#整体结构"><span class="nav-number">2.1.</span> <span class="nav-text">整体结构</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#结构解析"><span class="nav-number">2.2.</span> <span class="nav-text">结构解析</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#能力拆解"><span class="nav-number">2.3.</span> <span class="nav-text">能力拆解</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#深入-AntD-Form"><span class="nav-number">3.</span> <span class="nav-text">深入 AntD Form</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#AntD-Form-组件及能力"><span class="nav-number">3.1.</span> <span class="nav-text">AntD Form 组件及能力</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Form-结构"><span class="nav-number">3.2.</span> <span class="nav-text">Form 结构</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#InternalForm-相关内容"><span class="nav-number">3.3.</span> <span class="nav-text">InternalForm 相关内容</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#FormItem-相关内容"><span class="nav-number">3.4.</span> <span class="nav-text">FormItem 相关内容</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#可动手的点"><span class="nav-number">4.</span> <span class="nav-text">可动手的点</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#form-对象跨层级传递"><span class="nav-number">4.1.</span> <span class="nav-text">form 对象跨层级传递</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#兄弟组件事件处理"><span class="nav-number">4.2.</span> <span class="nav-text">兄弟组件事件处理</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#更为舒适的命名方式"><span class="nav-number">4.3.</span> <span class="nav-text">更为舒适的命名方式</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#参考文章"><span class="nav-number">5.</span> <span class="nav-text">参考文章</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="蔡胤"
      src="/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">蔡胤</p>
  <div class="site-description" itemprop="description">Just do IT!</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">61</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">15</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">31</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/kazehaiya" title="GitHub → https://github.com/kazehaiya" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i></a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:c763416559@163.com" title="Mail → mailto:c763416559@163.com" rel="noopener" target="_blank"><i class="fa fa-envelope fa-fw"></i></a>
      </span>
      <span class="links-of-author-item">
        <a href="https://weibo.com/Nora_Monocy" title="Weibo → https://weibo.com/Nora_Monocy" rel="noopener" target="_blank"><i class="fab fa-weibo fa-fw"></i></a>
      </span>
  </div>


  <div class="links-of-blogroll motion-element">
    <div class="links-of-blogroll-title"><i class="fa fa-link fa-fw"></i>
      友情链接
    </div>
    <ul class="links-of-blogroll-list">
        <li class="links-of-blogroll-item">
          <a href="http://liujinkai.com/" title="http://liujinkai.com/" rel="noopener" target="_blank">凯凯刘</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="https://weekly.75team.com/" title="https://weekly.75team.com/" rel="noopener" target="_blank">奇舞周刊</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="https://75.team/post/list" title="https://75.team/post/list" rel="noopener" target="_blank">奇舞团博客</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="https://tech.meituan.com/" title="https://tech.meituan.com/" rel="noopener" target="_blank">美团技术团队</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="https://tech.youzan.com/" title="https://tech.youzan.com/" rel="noopener" target="_blank">有赞技术团队</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="https://kalacloud.com" title="https://kalacloud.com" rel="noopener" target="_blank">卡拉云</a>
        </li>
    </ul>
  </div>

      </div>
        <div class="back-to-top motion-element">
          <i class="fa fa-arrow-up"></i>
          <span>0%</span>
        </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        
  <div class="beian"><a href="https://beian.miit.gov.cn" rel="noopener" target="_blank">京ICP备2020036147号-1 </a>
      <img src="/images/beian.png" style="display: inline-block;"><a href="http://www.beian.gov.cn/portal/registerSystemInfo?recordcode=42011702000651" rel="noopener" target="_blank">鄂公网安备 42011702000651 号 </a>
  </div>

<div class="copyright">
  
  &copy; 2018 – 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-dragon"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">蔡胤</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-area"></i>
    </span>
      <span class="post-meta-item-text">站点总字数：</span>
    <span title="站点总字数">101k</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
      <span class="post-meta-item-text">站点阅读时长 &asymp;</span>
    <span title="站点阅读时长">1:32</span>
</div>

        








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="//cdn.jsdelivr.net/gh/theme-next/theme-next-pjax@0/pjax.min.js"></script>
  <script src="//cdn.jsdelivr.net/npm/medium-zoom@1/dist/medium-zoom.min.js"></script>
  <script src="//cdn.jsdelivr.net/npm/lozad@1/dist/lozad.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>
<script src="/js/utils.js"></script><script src="/js/motion.js"></script>
<script src="/js/schemes/pisces.js"></script>
<script src="/js/next-boot.js"></script>
  <script>
var pjax = new Pjax({
  selectors: [
    'head title',
    '#page-configurations',
    '.content-wrap',
    '.post-toc-wrap',
    '.languages',
    '#pjax'
  ],
  switches: {
    '.post-toc-wrap': Pjax.switches.innerHTML
  },
  analytics: false,
  cacheBust: false,
  scrollTo : !CONFIG.bookmark.enable
});

window.addEventListener('pjax:success', () => {
  document.querySelectorAll('script[data-pjax], script#page-configurations, #pjax script').forEach(element => {
    var code = element.text || element.textContent || element.innerHTML || '';
    var parent = element.parentNode;
    parent.removeChild(element);
    var script = document.createElement('script');
    if (element.id) {
      script.id = element.id;
    }
    if (element.className) {
      script.className = element.className;
    }
    if (element.type) {
      script.type = element.type;
    }
    if (element.src) {
      script.src = element.src;
      // Force synchronous loading of peripheral JS.
      script.async = false;
    }
    if (element.dataset.pjax !== undefined) {
      script.dataset.pjax = '';
    }
    if (code !== '') {
      script.appendChild(document.createTextNode(code));
    }
    parent.appendChild(script);
  });
  NexT.boot.refresh();
  // Define Motion Sequence & Bootstrap Motion.
  if (CONFIG.motion.enable) {
    NexT.motion.integrator
      .init()
      .add(NexT.motion.middleWares.subMenu)
      .add(NexT.motion.middleWares.postList)
      .bootstrap();
  }
  NexT.utils.updateSidebarPosition();
});
</script>




  




  <script src="/js/local-search.js"></script>












    <div id="pjax">
  

  


<script>
NexT.utils.loadComments(document.querySelector('#valine-comments'), () => {
  NexT.utils.getScript('//cdn.jsdelivr.net/npm/valine@1/dist/Valine.min.js', () => {
    var GUEST = ['nick', 'mail', 'link'];
    var guest = 'nick,mail';
    guest = guest.split(',').filter(item => {
      return GUEST.includes(item);
    });
    new Valine({
      el         : '#valine-comments',
      verify     : true,
      notify     : true,
      appId      : 'GkgJm97N3UFm2mRb1eFcP2dn-gzGzoHsz',
      appKey     : 'kJ8tYUUkX4ifhWha8rjnxWS8',
      placeholder: "请不要吝惜你的笔头，来说点什么吧！",
      avatar     : 'mm',
      meta       : guest,
      pageSize   : '10' || 10,
      visitor    : false,
      lang       : 'zh-cn' || 'zh-cn',
      path       : location.pathname,
      recordIP   : true,
      serverURLs : ''
    });
  }, window.Valine);
});
</script>

    </div>
<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginRootPath":"live2dw/","pluginJsPath":"lib/","pluginModelPath":"assets/","tagMode":false,"debug":false,"model":{"jsonPath":"/live2dw/assets/Epsilon2.1.model.json"},"display":{"position":"right","width":150,"height":300},"mobile":{"show":true,"scale":0.5},"react":{"opacity":0.7},"log":false});</script></body>
</html>
